package com.example.Holo.uiactivities.fragments;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.*;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.Holo.R;
import com.example.Holo.core.BaseApplication;
import com.example.Holo.library.viewfinder.IViewFinder;
import com.example.Holo.rework.services.interfaces.IPaymentService;
import com.example.Holo.rework.services.interfaces.IServiceFactory;

import static com.example.Holo.library.viewfinder.ViewFinders.getViewFinder;
import static com.example.Holo.rework.dao.implementation.sqlite.android.Column.NAME_COLUMN;
import static com.example.Holo.rework.dao.implementation.sqlite.android.Column.QUANTITY_COLUMN;

public class ExpensesFragment extends BaseFragment {

    private static final String[] FROM = new String[] {
            NAME_COLUMN.name,
            QUANTITY_COLUMN.name
    };

    private static final int[] TO = new int[] {
            R.id.name_item,
            R.id.quantity_item
    };

    private BaseApplication baseApplication;
    private Cursor allPaymentsCursor;
    private SimpleCursorAdapter adapter;

    @Override
    public String getFragmentTitle() {
        return "Expenses";
    }

    @Override
    public int getActivityMenuResource() {
        return R.menu.expenses_fragment_menu;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.expenses_general_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final IViewFinder viewFinder = getViewFinder(activity);
        final ListView listView = viewFinder.findListView(R.id.all_expenses);
        baseApplication = (BaseApplication) activity.getApplicationContext();
        final IServiceFactory serviceFactory = baseApplication.getServiceFactory();
        final IPaymentService paymentService = serviceFactory.createPaymentService();
        allPaymentsCursor = paymentService.getAllPayments(baseApplication.getEventId());
        adapter = new SimpleCursorAdapter(activity, R.layout.name_layout, allPaymentsCursor, FROM, TO, 0);
        listView.setAdapter(adapter);
        registerForContextMenu(listView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        final MenuInflater menuInflater = activity.getMenuInflater();
        menuInflater.inflate(R.menu.event_list_view_context_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        final IServiceFactory serviceFactory = baseApplication.getServiceFactory();
        final IPaymentService paymentService = serviceFactory.createPaymentService();
        paymentService.deletePayment(menuInfo.id);
        allPaymentsCursor.requery();
        adapter.notifyDataSetChanged();
        return true;
    }
}
